const { ElMessage, ElMessageBox } = ElementPlus

export function useContainerView() {
  const openContainerViewPage = configObj => {
    const { activeMenuName, unCheckDetailListRef, childContainerTableRef, containerInfo, currentSelectContainer, userInfo } = configObj;
    const unCheckSelectRows = unCheckDetailListRef.value.getSelectionRows();//【待检验信息】勾选的行
    const childContainerSelectRows = childContainerTableRef.value.getSelectionRows();//【子批次号】勾选的行
    let selectRows = activeMenuName == '待检验' ? unCheckSelectRows : childContainerSelectRows;
    const { containertype } = containerInfo;

    if (activeMenuName == '待检验' && unCheckSelectRows.length === 0) {
      return ElMessage({ type: 'error', message: `请选择批次`, grouping: true });
    }

    if (activeMenuName != '待检验' && !containerInfo.containerstepdispatchid) {
      return ElMessage({ type: 'error', message: `请选择批次`, grouping: true });
    }

    if (containertype === 7 && selectRows.length === 0) {
      return ElMessage({ type: 'error', message: `请选择批次`, grouping: true });
    }

    ElMessageBox.confirm(`即将跳转并打开【流水卡预览】页面，是否继续?`, '确认', { type: 'warning' }).then(async () => {
      const { workflowid, containerid } = currentSelectContainer;
      let containeridList = [];
      let reportData = {};

      if (containertype === 7) {
        for (const i of selectRows) {
          containeridList.push(i.containerid);
        }
        reportData = { containeridList, workflowid, IsBomPrint: 0 };
      } else {
        reportData = { containeridList: [containerid], workflowid, IsBomPrint: 0 };
      }

      const containerData = Encrypt(JSON.stringify(reportData));
      const uname = Encrypt(userInfo.value.employeename);
      let hostname = window.location.hostname === 'localhost' ? 'localhost' : '10.15.7.75';
      const url = `http://${hostname}/insitewebapplication/forms/Custom/Print/ContainerInfoPrintPage.html?containerData=${containerData}&uname=${uname}`
      window.open(url);
    }).catch(() => {
      ElMessage({ type: 'info', message: '已取消' });
    });
  }

  return {
    openContainerViewPage
  };
}